import sys

from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt, pyqtSignal, QObject
from PyQt5.QtWidgets import QWidget, QDesktopWidget, QApplication, QLabel, QMessageBox, QLineEdit, QPushButton, QAction, \
    qApp, QMainWindow, QTextEdit

"""
界面风格
2020.5.8
"""
LEFT_SPACE = 10
TOP_SPACE = 50

class Example(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.resize(1000, 800)
        self.center()
        self.def_init()

        self.setWindowTitle('Center')
        # 设置窗口的图标，引用当前目录下的web.png图片
        self.setWindowIcon(QIcon('logo.jpg'))
        self.show()

    def def_init(self):
        # 状态栏
        self.statusBar().showMessage('   Ready!')

        #  创建一个动作
        exitAction = QAction(QIcon('exit.jpg'), 'Exit', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.setStatusTip('Exit application')
        exitAction.triggered.connect(qApp.quit)
        # self.toolbar = self.addToolBar('Exit')
        # self.toolbar.addAction(exitAction)

        # 创建一个菜单栏
        menubar = self.menuBar()
        # 添加菜单
        fileMenu = menubar.addMenu('&File')
        # 添加事件
        fileMenu.addAction(exitAction)

        # 工具栏
        self.toolbar = self.addToolBar('Exit')
        self.toolbar.addAction(exitAction)

        lbl1 = QLabel('网卡', self)
        lbl1.move(LEFT_SPACE+40, TOP_SPACE+30)
        lbl2 = QLabel('规则', self)
        lbl2.move(LEFT_SPACE+40, TOP_SPACE+60)

        edt1 = QLineEdit(self)
        edt1.resize(400, 23)
        edt1.move(LEFT_SPACE+100, TOP_SPACE+30+5)
        edt2 = QLineEdit(self)
        edt2.resize(400, 23)
        edt2.move(LEFT_SPACE+100, TOP_SPACE+60+5)

        btn1 = QPushButton("开始捕获", self)
        btn1.resize(120, 30)
        btn1.move(LEFT_SPACE+600, TOP_SPACE+50)
        btn2 = QPushButton("结束捕获", self)
        btn2.resize(120, 30)
        btn2.move(LEFT_SPACE+800, TOP_SPACE+50)
        btn1.clicked.connect(self.buttonClicked)
        btn2.clicked.connect(self.buttonClicked)

        txtedt = QTextEdit(self)
        txtedt.resize(900, 260)
        txtedt.move(LEFT_SPACE+40, TOP_SPACE+110)

        txtedt1 = QTextEdit(self)
        txtedt1.resize(300, 330)
        txtedt1.move(LEFT_SPACE + 40, TOP_SPACE + 390)

        txtedt2 = QTextEdit(self)
        txtedt2.resize(580, 200)
        txtedt2.move(LEFT_SPACE + 360, TOP_SPACE + 390)

        lbl3 = QLabel('数据统计', self)
        lbl3.move(LEFT_SPACE + 360, TOP_SPACE + 600)

        space = 60
        left = 360
        lbl4 = QLabel('TCP包', self)
        lbl4.move(LEFT_SPACE + left, TOP_SPACE + 630)
        edt4 = QLineEdit(self)
        edt4.resize(80, 23)
        edt4.move(LEFT_SPACE + left + space, TOP_SPACE + 630 + 5)

        lbl5 = QLabel('UDP包', self)
        lbl5.move(LEFT_SPACE + left, TOP_SPACE + 660)
        edt5 = QLineEdit(self)
        edt5.resize(80, 23)
        edt5.move(LEFT_SPACE + left + space, TOP_SPACE + 660 + 5)

        lbl6 = QLabel('ICMP包', self)
        lbl6.move(LEFT_SPACE + left, TOP_SPACE + 690)
        edt6 = QLineEdit(self)
        edt6.resize(80, 23)
        edt6.move(LEFT_SPACE + left + space, TOP_SPACE + 690 + 5)

        left = left + 160
        lbl7 = QLabel('HTTP包', self)
        lbl7.move(LEFT_SPACE + left, TOP_SPACE + 630)
        edt7 = QLineEdit(self)
        edt7.resize(80, 23)
        edt7.move(LEFT_SPACE + left + space, TOP_SPACE + 630 + 5)

        lbl8 = QLabel('ARP包', self)
        lbl8.move(LEFT_SPACE + left, TOP_SPACE + 660)
        edt8 = QLineEdit(self)
        edt8.resize(80, 23)
        edt8.move(LEFT_SPACE + left + space, TOP_SPACE + 660 + 5)

        lbl9 = QLabel('IPV4包', self)
        lbl9.move(LEFT_SPACE + left, TOP_SPACE + 690)
        edt9 = QLineEdit(self)
        edt9.resize(80, 23)
        edt9.move(LEFT_SPACE + left + space, TOP_SPACE + 690 + 5)

        left = left + 160
        space = 40
        lbl10 = QLabel('其它', self)
        lbl10.move(LEFT_SPACE + left, TOP_SPACE + 630)
        edt10 = QLineEdit(self)
        edt10.resize(80, 23)
        edt10.move(LEFT_SPACE + left + space, TOP_SPACE + 630 + 5)

        lbl11 = QLabel('合计', self)
        lbl11.move(LEFT_SPACE + left, TOP_SPACE + 660)
        edt11 = QLineEdit(self)
        edt11.resize(80, 23)
        edt11.move(LEFT_SPACE + left + space, TOP_SPACE + 660 + 5)

    def closeEvent(self, event):
        reply = QMessageBox.question(self, 'Message',
                                     "Are you sure to quit?", QMessageBox.Yes |
                                     QMessageBox.No, QMessageBox.No)

        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()

    def keyPressEvent(self, e):
        if e.key() == Qt.Key_Escape:
            self.close()

    def mousePressEvent(self, event):
        self.statusBar().showMessage('   Ready!')

    def buttonClicked(self):
        sender = self.sender()
        self.statusBar().showMessage("  " + sender.text() + ' was pressed')


    # 控制窗口显示在屏幕中心的方法
    def center(self):
        # 获得窗口
        qr = self.frameGeometry()
        # 获得屏幕中心点
        cp = QDesktopWidget().availableGeometry().center()
        # 显示到屏幕中心
        qr.moveCenter(cp)
        self.move(qr.topLeft())


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())